package si.yun.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import si.yun.dao.SyActicleCategrayMapper;
import si.yun.service.syActicleSe;
import si.yun.utils.CacheKeyManager;
import si.yun.utils.GuavaUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import static si.yun.utils.CacheKeyManager.INDEX_ActcleTree;

/**
 *  @ClassName: 实现类
 *  @Description: TODO 
 *  @Author: 张雨薇
 *  @Date 2021/7/29 11:28
 */
@Service
public class syActcleImpl implements syActicleSe {
    @Autowired
    private SyActicleCategrayMapper dao;

    @Autowired
    public GuavaUtils baseCache;

    /**
      * @Author : 张雨薇
      * @Date : 2021/7/29 11:29
      * @Param : [code]
      * @Return : java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
      */
    @Override
    public List<Map<String, Object>> seActcleTree(int code) {
        String curriculumTree = String.format(CacheKeyManager.INDEX_ActcleTree, code);
        Object cacheObject = null;
        try {
            cacheObject = baseCache.getTenCacheBuild().get(curriculumTree, () -> {
                List<Map<String, Object>> ztreeNodes = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                map.put("id", 1);
                map.put("name", "选择分类类型");
                map.put("open", true);
                List<Map<String, Object>> children1 = dao.seActcleTree(code);
                for (Map<String, Object> c : children1) {

                    String str = (String) c.get("name");
                    ;
                    if (str.length() > 7) {
                        String name = str.substring(0, 7) + ".....";
                        c.put("name", name);
                    } else {
                        c.put("name", str);
                    }

                    c.put("open", true);
                    int id = (int) c.get("id");
                    List<Map<String, Object>> maps = seManageDtreeOf(id);
                    c.put("children", maps);

                    if (c.get("id").equals(id)) {
                        c.put("checked", true);
                    }
                }
                map.put("children", children1);
                ztreeNodes.add(map);
                return ztreeNodes;
            });
            if (cacheObject instanceof List) {
                List<Map<String, Object>> ztreeNodes = (List<Map<String, Object>>) cacheObject;
                return ztreeNodes;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Map<String,Object>> seManageDtreeOf(int id){
        List<Map<String, Object>> children = dao.seActcleTree(id);
        if(children!=null){
            for(Map<String, Object> c:children){

                String str =(String) c.get("name");;
                if(str.length()>7){
                    String name=str.substring(0,7)+".....";
                    c.put("name",name);
                }else{
                    c.put("name",str);
                }

                c.put("open",true);
                int id1= (int) c.get("id");
                List<Map<String, Object>> maps = seManageDtreeOf(id1);
                c.put("children",maps);
            }
        }
        return children;

    }

    /**
      * @Author : ztree的修改
      * @Date : 2021/7/30 15:51
      * @Param : [Newname, id]
      * @Return : int
      */
    @Override
    public int upActTree(String Newname, Integer id,Integer updateUser) {
        int i = dao.upActTree(Newname, id,updateUser);
        String curriculumTree=String.format(CacheKeyManager.INDEX_ActcleTree);
        if(i>0){
            baseCache.invalidate(curriculumTree);
        }
        return i;
    }

    /**
      * @Author : ztree的删除
      * @Date : 2021/7/30 16:20
      * @Param : [acticleCategoryId]
      * @Return : int
      */
    @Override
    public int deleteByPrimaryKey(Integer acticleCategoryId) {
        int i = dao.deleteByPrimaryKey(acticleCategoryId);
        String curriculumTree=String.format(CacheKeyManager.INDEX_ActcleTree);
        if(i>0){
            baseCache.invalidate(curriculumTree);
        }
        return i;
    }

    /**
      * @Author : 张雨薇
      * @Date : 2021/7/30 17:02
      * @Param : [name, parentId]
      * @Return : int
      */
    @Override
    public int inActTree(String name, Integer parentId,Integer createUser) {
        int i = dao.inActTree(name, parentId,createUser);
        String curriculumTree=String.format(CacheKeyManager.INDEX_ActcleTree);
        if(i>0){
            baseCache.invalidate(curriculumTree);
        }
        return i;
    }

    /**
      * @Author : 查询表
      * @Date : 2021/8/3 17:13
      * @Param : []
      * @Return : java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
      */
    @Override
    public List<Map<String, Object>> seActcleTab() {
        List<Map<String, Object>> maps = dao.seActcleTab();
        return maps;
    }

}
